package com.hmyd.hmyd_android.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.MessageDetail;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 消息详情
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_message_detail)
public class MessageDetailActivity extends BasicActivity {
	private Context mContext;
	private String mMessageId;
	private String tag;// 用于判断是普通消息还是积分商城消息
	private TextView mTvTitle;
	private TextView mTvSender;
	private TextView mTvTime;
	private TextView mTvContent;
	private static final int MESSAGE_DETAIL = 1;
	private final int READED = 2;

	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	public static Intent getIntent(Context context, String messageId, String tag) {
		Intent intent = new Intent(context, MessageDetailActivity.class);
		intent.putExtra("messageId", messageId);
		intent.putExtra("tag", tag);

		return intent;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		init();

	}

	private void init() {
		mContext = this;
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		mTvTime = (TextView) findViewById(R.id.tv_time);
		mTvContent = (TextView) findViewById(R.id.tv_content);
		mTvSender = (TextView) findViewById(R.id.tv_sender);
		
		RequestParams params = new RequestParams();
		params.addBodyParameter("messageId", mMessageId);

		// 普通消息
		onRequest(MESSAGE_DETAIL, UrlConstant.MESSAGE_DETAIL, HttpMethod.GET, params);
		onRequest(READED, UrlConstant.READED, HttpMethod.GET, params);

	}

	@Override
	public void onPostSuccess(int postId, String result) {

		switch (postId) {
		case MESSAGE_DETAIL:
			Gson gson = new Gson();
			MessageDetail messageDetail = gson.fromJson(result, MessageDetail.class);

			mTvContent.setText(messageDetail.content);
			mTvSender.setText(messageDetail.sender);
			mTvTime.setText(Utils.getDateString(messageDetail.sendTime));
			mTvTitle.setText(messageDetail.subject);
			break;

		default:
			break;
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("我的消息");
		mMessageId = getIntent().getStringExtra("messageId");

	}

	@Override
	protected void leftButton() {
	AppBasic.getInstance().finishCurrentActivity();

	}

	@Override
	protected void rightButton() {

	}

}
